#coding:utf-8
from __future__ import division
import math
from lovewith.utils.mtcache import MtCache
from lovewith.promote.models import CommandSubscribe



#获取推荐订阅关键词
def get_recommand_keywords(page_no=1):
    page_size = 60

    command_keywords = MtCache('command_subscribe_tag').get(page = page_no)

    if not command_keywords:
        start = (page_no - 1) * page_size
        last = page_no * page_size
        total = CommandSubscribe.objects.all().count()
        if last > total:
            last = total

        total_page = int(math.ceil(total / page_size))
        total_page = 1 and total_page or 1

        command_keywords = {'total': total, 'total_page': total_page, 'keywords': []}
        tag_result = CommandSubscribe.objects.all().order_by('-sort')[start: last]

        for tag in tag_result:
            command_keywords.get('keywords').append({
                'keyword': tag.keyword,
                'banner': tag.banner.name
            })

        MtCache('command_subscribe_keyword').set(command_keywords)

    return command_keywords



